import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'
import { menuItems, type MenuItems } from '@/layout/menu/menu'
import LoginPage from '../views/LoginView.vue'
// 创建一个函数来构建路由配置
const buildRoutes = (items: MenuItems[]): RouteRecordRaw[] => {
  const result: RouteRecordRaw[] = []
  items.forEach((item) => {
    result.push({
      path: '/' + item.key,
      name: item.key,
      component: () => import(`../views/HomeView2.vue`),
      meta: { layout: 'main' }
    })
    if (item.children && Array.isArray(item.children)) {
      item.children.forEach((element) => {
        result.push({
          path: '/' + element.key,
          name: element.key,
          component: () => import(`../views/HomeView2.vue`),
          meta: { layout: 'main' }
        })
        if (element.children && Array.isArray(element.children)) {
          element.children.forEach((childItem) => {
            result.push({
              path: '/' + childItem.route,
              name: childItem.key,
              component: () => import(`../views/CommonView.vue`),
              meta: { layout: 'main' }
            })
          })
        } else {
          result.push({
            path: '/' + element.route,
            name: element.key,
            component: () => import(`../views/CommonView.vue`),
            meta: { layout: 'main' }
          })
        }
      })
    }
  })

  return [
    {
      path: '/login',
      name: 'login',
      component: LoginPage, // 使用 SimpleLayout,
      meta: { layout: 'login' }
    },
    {
      path: '/home',
      name: 'opt',
      component: () => import('../views/HomeView.vue'),
      meta: { layout: 'main' }
    },
    ...result
  ]
}

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: buildRoutes(menuItems)
})

export default router
